<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="searchForm" class="mb-20">
      <el-form-item label="关键词">
        <el-input v-model="searchForm.keyword" placeholder="输入关键词搜索" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 评论表格 -->
    <el-table :data="commentList" border style="width: 100%">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="words" label="评论内容" show-overflow-tooltip min-width="200" align="center" />
      <el-table-column label="用户昵称" width="120" align="center">
        <template slot-scope="scope">
          {{ UserNameList[scope.$index] }}
        </template>
      </el-table-column>
      <el-table-column label="作品名称" width="120" align="center">
        <template slot-scope="scope">
          {{ OpusNameList[scope.$index] }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="评论时间" width="160" align="center">
        <template #default="scope">
          {{ formatTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template #default="scope">
          <el-button type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" plain @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="mt-20"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      :current-page="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 编辑弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="commentForm" :rules="rules" label-width="120px">
        <el-form-item label="评论内容" prop="words">
          <el-input v-model="commentForm.words" type="textarea" :rows="3" placeholder="请输入评论内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmitEdit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getList, deleteComment, updateComment } from '@/api/Comment' // 后期替换为真实接口
import moment from 'moment'

export default {
  name: 'CommentManage',
  data() {
    return {
      commentList: [],
      OpusNameList: [],
      UserNameList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      searchForm: {
        keyword: ''
      },
      dialogVisible: false,
      dialogTitle: '编辑评论',
      commentForm: {
        commentId: null,
        words: ''
      },
      rules: {
        words: [{ required: true, message: '评论内容不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取评论列表
    async fetchData() {
      try {
        const res = await getList({
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...this.searchForm
        })
        this.commentList = res.opusCommentPage.records
        this.total = res.opusCommentPage.total
        this.OpusNameList = res.OpusNameList
        this.UserNameList = res.UserNameList
      } catch (error) {
        this.$message.error('获取评论列表失败')
      }
    },
    // 时间格式化
    formatTime(time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 搜索
    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        words: '',
        commentUserId: null,
        opusId: null
      }
      this.currentPage = 1
      this.fetchData()
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 编辑评论
    handleEdit(row) {
      this.commentForm = { ...row }
      this.dialogVisible = true
    },
    // 提交编辑
    async handleSubmitEdit() {
      try {
        await updateComment(this.commentForm)
        this.$message.success('评论更新成功')
        this.dialogVisible = false
        await this.fetchData()
      } catch (error) {
        this.$message.error('更新失败')
      }
    },
    // 删除评论
    handleDelete(row) {
      this.$confirm('确定删除该评论?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await deleteComment({ commentId: row.commentId })
          this.$message.success('删除成功')
          this.fetchData()
        } catch (error) {
          this.$message.error('操作失败')
        }
      })
    }
  }
}
</script>

<style scoped>
.mb-20 {
  margin-bottom: 20px;
}

.mt-20 {
  margin-top: 20px;
}

.content-preview {
  white-space: pre-line;
  max-width: 300px;
}
</style>
